package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.PictureAdapter;
import flc.ast.databinding.ActivityPictureBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class PictureActivity extends BaseAc<ActivityPictureBinding> {

    private PictureAdapter pictureAdapter;
    private boolean isEdit = false;
    private List<String> listUri = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_picture;
    }

    @Override
    public void onBackPressed() {
        if (isEdit) {
            cancelEdit();
        } else {
            finish();
        }
    }

    @Override
    protected void initView() {
        mDataBinding.ivPictureBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivPictureEdit.setOnClickListener(this);
        mDataBinding.tvPictureDelete.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(mContext, 3));
        pictureAdapter = new PictureAdapter();
        mDataBinding.rvPictureList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPictureEdit://编辑
                isEdit = true;
                mDataBinding.ivPictureEdit.setVisibility(View.GONE);
                mDataBinding.tvPictureDelete.setVisibility(View.VISIBLE);
                pictureAdapter.setEdit(isEdit);
                pictureAdapter.notifyDataSetChanged();
                mDataBinding.tvPictureDelete.setText(getString(R.string.delete_text, listUri.size()));
                break;
            case R.id.tvPictureDelete://删除
                if (listUri.size() == 0) return;
                deletePicture();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (pictureAdapter.getItem(position).isChecked()) {
                pictureAdapter.getItem(position).setChecked(false);
                listUri.remove(pictureAdapter.getItem(position).getUri());
            } else {
                pictureAdapter.getItem(position).setChecked(true);
                listUri.add(pictureAdapter.getItem(position).getUri());
            }
            pictureAdapter.notifyDataSetChanged();
            mDataBinding.tvPictureDelete.setText(getString(R.string.delete_text, listUri.size()));
        } else {
            Uri imageUri = Uri.parse(pictureAdapter.getItem(position).getUri());
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(imageUri, "image/*");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.tvPictureNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                    mDataBinding.tvPictureDelete.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    //删除图片
    private void deletePicture() {

        showDialog(getString(R.string.delete_ing));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                for (String uri : listUri) {
                    mContext.getContentResolver().delete(Uri.parse(uri), null, null);
                }
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dismissDialog();
                        cancelEdit();
                        getData();
                        ToastUtils.showShort(getString(R.string.delete_suc));
                    }
                }, 500);
            }
        });
    }

    //取消编辑
    private void cancelEdit() {
        listUri.clear();
        isEdit = false;
        mDataBinding.ivPictureBack.setVisibility(View.VISIBLE);
        mDataBinding.ivPictureEdit.setVisibility(View.VISIBLE);
        mDataBinding.tvPictureDelete.setVisibility(View.GONE);
        pictureAdapter.setEdit(isEdit);
        for (SelectMediaEntity bean : pictureAdapter.getValidData()) {
            bean.setChecked(false);
        }
        pictureAdapter.notifyDataSetChanged();
    }

}